<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    protected $refreshBtn;

    public function __construct()
    {
        $this->refreshBtn = $this->getRefreshBtn();
    }

    public function getRefreshBtn()
    {
        $message_refresh_succeeded = trans('admin::lang.refresh_succeeded');

        return <<<EOT
<a class="btn btn-sm btn-primary grid-refresh pull-right"><i class="fa fa-refresh"></i> 刷新</a>
<script type="text/javascript">
$('.grid-refresh').on('click', function() {
    $.pjax.reload('#pjax-container');
    toastr.success('{$message_refresh_succeeded}');
});
</script>
EOT;
    }


    /**
     * 获取指定日期段内每一天的日期
     * @param  Date $startdate 开始日期
     * @param  Date $enddate 结束日期
     * @return Array
     */
    function getDateFromRange($startdate, $enddate)
    {

        $stimestamp = strtotime($startdate);
        $etimestamp = strtotime($enddate);

        // 计算日期段内有多少天
        $days = ($etimestamp - $stimestamp) / 86400 + 1;

        // 保存每天日期
        $date = array();

        for ($i = 0; $i < $days; $i++) {
            $date[] = date('Y-m-d', $stimestamp + (86400 * $i));
        }

        return $date;
    }

    /**
     * 获取指定日期段内每一月
     * @param  Date $startdate 开始日期
     * @param  Date $enddate 结束日期
     * @return Array
     */
    function getMonthFromRange($startdate, $enddate)
    {


        $start_arr = explode("-", $startdate);
        $end_arr = explode("-", $enddate);

        $start_year = intval($start_arr[0]);
        $start_month = intval($start_arr[1]);

        $end_year = intval($end_arr[0]);
        $end_month = intval($end_arr[1]);

        $diff_year = $end_year-$start_year;

        $month_arr = [];
//获取月份
        if($diff_year == 0){
            for($month = $start_month;$month<=$end_month;$month++){
                $month_arr[] = date('Ym', strtotime($start_year.'-'.$month.'-1'));
            }
        } else {
            for($year =$start_year;$year<=$end_year;$year++){
                if($year == $start_year){
                    for($month = $start_month;$month<=12;$month++){
                        $month_arr[] = date('Ym', strtotime($year.'-'.$month.'-1'));
                    }
                }elseif($year==$end_year){
                    for($month = 1;$month<=$end_month;$month++){
                        $month_arr[] = date('Ym', strtotime($year.'-'.$month.'-1'));
                    }
                }else{
                    for($month = 1;$month<=12;$month++){
                        $month_arr[] = date('Ym', strtotime($year.'-'.$month.'-1'));
                    }
                }
            }
        }
//        print_r($month_arr);
        return $month_arr;
    }
}
